<?php
/** 初始化项目 */
require '../../app/app.php';
/** 初始化数据库 */
use app\util\BackupMySQL;
use app\util\Mysql;
$database = Mysql::init();

if (isset($_SESSION['username']) && $_SESSION['username'] != "") {
    // 设置页面执行时间
    set_time_limit(0);
    // 设置内存无限制
    ini_set('memory_limit', '-1');


    $config = DATABASE_CONFIG['mysql'];
    $backup_path = APP_PATH.'/backup/';
    $backup = new BackupMySQL($config['hostname'],$config['username'],$config['password'],$config['database'],$backup_path);
    try {
        $backup->backup();
        $username=$_SESSION["username"];
        $client_ip = getClientIP();
        $timestamp = time();
        $database->insert("secure_info",["user"=>$username,"ip"=>$client_ip,"times"=>$timestamp,"content"=>"备份了数据库。"]);
        echo '数据库已备份';
    } catch (\Exception $e) {
        echo '备份失败：'.$e->getMessage();
    }
}
